﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace JControlEx.Common
{
    [ValueConversion(typeof(byte[]), typeof(ImageSource))]
    public class ByteImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is byte[]))
            {
                return null;
            }
            MemoryStream stream = new MemoryStream(value as byte[]);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = stream;
            bi.EndInit();
            return bi;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(byte[]), typeof(string))]
    public class ByteStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is byte[]) || !(parameter is System.Windows.Controls.Panel))
            {
                return null;
            }
            string param = (parameter as System.Windows.Controls.Panel).Tag as string;

            byte[] val = value as byte[];
            if (param == "Hex")
            {
                return ByteArrayToString(val);
            }
            else if (param == "Base64")
            {
                return System.Convert.ToBase64String(val);
            }
            else if (param == "IntArray")
            {
                return String.Join(" ", val);
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is string) || !(parameter is System.Windows.Controls.Panel))
            {
                return null;
            }
            string param = (parameter as System.Windows.Controls.Panel).Tag as string;

            string val = value as string;
            if (param == "Hex")
            {
                return StringToByteArray(val);
            }
            else if (param == "Base64")
            {
                return System.Convert.FromBase64String(val);
            }
            else if (param == "IntArray")
            {
                return val.Split(' ').Select(s => byte.Parse(s)).ToArray();
            }
            else
            {
                return null;
            }
        }

        public static string ByteArrayToString(byte[] ba)
        {
            string hex = BitConverter.ToString(ba);
            return hex.Replace('-', ' ');
        }

        public static byte[] StringToByteArray(string s)
        {
            return s.Split(' ').Select(str => byte.Parse(System.Convert.ToInt32(str, 16).ToString())).ToArray();
        }
    }

    [ValueConversion(typeof(long), typeof(DateTime))]
    public class DateTimeConverter : IValueConverter
    {
        public DateTimeConverter(Func<object, DateTime> convert)
        {

        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
